package pers.lance.jmsproject.util;

import org.apache.commons.dbcp.BasicDataSource;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @author Lance Li
 * @date 7/10/2018 2:06 PM
 * @desc
 */
public class DBUtil {

    static BasicDataSource dataSource = new BasicDataSource();
    static Properties conf = new Properties();

    static {
        try {
            conf.load(new FileInputStream(""));
            String url = conf.getProperty("");
            String username = conf.getProperty("username");
            String password = conf.getProperty("password");
            String driverClassName = conf.getProperty("driverClassName");

            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.setDriverClassName(driverClassName);
            dataSource.setInitialSize(10);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static Connection getConnectionWithDpc() {
        String url = "jdbc:oracle:thin:@localhost:1521:xe";
        String username = "lance";
        String password = "123456";
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            return DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Connection getConnectionWithDbcp() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void close(Connection con, Statement st, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
